package com.alibaba.basic.controller;

import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.IVerifyCodeService;
import com.alibaba.basic.util.AjaxResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/verifyCode")
public class ImageVerifyCodeController {
    @Resource
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}") //所有要获取图片验证码都可以使用
    public AjaxResult imgCode(@PathVariable("key") String key){
        try {
            String imageBase64Str = verifyCodeService.getImgCode(key);
            return AjaxResult.me().setResultObj(imageBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
